package com.systop.busafter.admin.controller;
import com.systop.commer.Io;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.Admin;
import com.systop.busafter.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;
    //修改
    @RequestMapping(value="/adminupdate",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult adminupdate(@RequestBody Admin admin){
        String encode = Io.encode(admin.getAdmin_password().getBytes());
        admin.setAdmin_password(encode);
        int i =  adminService.updataadmin(admin);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }

//    String string = "qianyang123";
//    //编码
//    String encode = Io.encode(string.getBytes());
//        System.out.println(string + "\t编码后的字符串为：" + encode);
//    //解码
//    String decode = Io.decode(encode.getBytes());
//        System.out.println(encode + "\t字符串解码后为：" + decode);

    //添加
    @RequestMapping(value="/insertAdmin",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult insertAdmin(@RequestBody Admin admin1){
        String name = admin1.getAdmin_name_n();
        List<Admin> list =  adminService.adminListall();
        String a="";
        for (int i=0;i<list.size();i++){
            a=list.get(i).getAdmin_name_n();
            //equals比较可以，== 存在误区
           if ( a.equals(name)){
               return ResponseResult.build(20, "添加重复，重复");
           }
        }
        //编码adminListall
        String encode = Io.encode(admin1.getAdmin_password().getBytes());
        admin1.setAdmin_password(encode);
        int ii =  adminService.insertAdmin(admin1);
        if (ii == 0) {
            return ResponseResult.build(500, "添加失败");
        } else {
            return ResponseResult.build(200, "添加成功", ii);
        }
    }

    //登陆
    @RequestMapping(value="/adminLogin",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult adminLogin(@RequestBody Admin admin){
        String encode = Io.encode(admin.getAdmin_password().getBytes());
        admin.setAdmin_password(encode);
        Admin admin1  =  adminService.adminLogin(admin);
        if (admin1 == null) {
            return ResponseResult.build(500, "登陆失败");
        } else {
            return ResponseResult.build(200, "登陆成功", admin1);
        }
    }
    //查看全部
    @RequestMapping(value="/adminListall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult adminListall(){
        List<Admin> list =  adminService.adminListall();
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过id查
    @RequestMapping(value="/adminIdlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult adminIdlist(@RequestBody Admin admin){
        List<Admin> list =  adminService.adminIdlist(admin.getAdmin_id());
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //删除
    @RequestMapping(value="/deleteAdmin",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deleteAdmin(@RequestBody Admin admin){
       int i =  adminService.deleteAdmin(admin.getAdmin_id());
        if (i == 0) {
            return ResponseResult.build(500, "删除失败");
        } else {
            return ResponseResult.build(200, "删除成功", i);
        }
    }

}
